<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ include file="/public/header.jsp"%>
<%
	String isSP = request.getParameter("isSP");
	String status = request.getParameter("status") == null ? "" : request.getParameter("status");
	if("".equals(status)){
		status = "0";
	}
	String gd_id = request.getParameter("gd_id") == null ? "" : request.getParameter("gd_id");
	String gd_content_id = request.getParameter("gd_content_id") == null ? "" : request.getParameter("gd_content_id");
	String gd_code = request.getParameter("gd_code") == null ? "" : request.getParameter("gd_code");
	String gd_name = request.getParameter("gd_name") == null ? "" : request.getParameter("gd_name");
	String xm_code = request.getParameter("xm_code") == null ? "" : request.getParameter("xm_code");
	String xm_name = request.getParameter("xm_name") == null ? "" : request.getParameter("xm_name");
%>
<html>
	<head>
		<title>乙供材料平衡表</title>
		<script type="text/javascript" src="<%=contextPath %>/public/js/extend.js"></script>
		<style>
			.td1{
				padding-top:10px;
				padding-bottom:5px;
				font-size: 12px;
				background-color: #c0ebd7;
				width:20%;
			}
			.td2{
				padding:5px;
			}
		</style>
	<script type="text/javascript">
		var lastIndex;
		var submitUrl = '<%=contextPath %>/springMVC/gd/addGDMaterialBalance1';
		$(function(){
			var url = '<%=contextPath %>/springMVC/gd/listGDMaterialBalance?type=1&gd_id=<%=gd_id%>&gd_content_id=<%=gd_content_id%>';
			$.ajax({
				type:'POST',
				url:'<%=contextPath %>/springMVC/gd/hasBalance?type=1&gd_id=<%=gd_id%>',
				async:false,
				success:function(rtjson){
					if(rtjson != '0'){
						url = '<%=contextPath %>/springMVC/gd/listGDMaterialBalanceByGdId?type=1&gd_id=<%=gd_id%>&gd_content_id=<%=gd_content_id%>';
						submitUrl = '<%=contextPath %>/springMVC/gd/updateGDMaterialBalance1';
						$('#submitValue').html("重新保存");
					}
				}
			});
			$('#table').datagrid({
				title:'绿色背景表示该材料使用量低于管控最小值',
				url:url,
				rownumbers:true,
				singleSelect:true,
				fitColumns:true,
				showFooter:true,
				frozenColumns:[[
				    {field:'id',hidden:true},
				    {title:'产品id',field:'material_id',hidden:true}
				]],
				columns:[[
					{title:'产品名称',field:'name',width:150,align:'center',rowspan:2},
					{title:'规格/型号',field:'format_type',width:150,align:'center',rowspan:2},
					{title:'单位',field:'unit_name',width:50,align:'center',rowspan:2},
					{title:'预算数量',field:'assume_num',width:100,align:'center',rowspan:2},
					{title:'管控精度(%)',field:'control',width:100,align:'center',rowspan:2},
					{title:'最小管控值',field:'min_num',width:100,align:'center',rowspan:2,formatter:function(value,row,index){
						if(row.assume_num == undefined){
							return;
						}
						var assume_num = parseFloat(row.assume_num);
					    if(assume_num == 0){
					    	return 0;
					    }
			    	    var control = parseFloat(row.control)/100;
			    	    var min_num = Math.round(assume_num*(1-control)*Math.pow(10,2))/Math.pow(10,2);
			    	    return  min_num;
					}},
					{title:'已用合计',field:'already_use',width:100,align:'center',rowspan:2},
					{title:'订单',width:300,align:'center',colspan:2},
					{title:'工余',width:200,align:'center',colspan:2},
					{title:'现场实用数',field:'xc_use',width:150,align:'center',rowspan:2,editor:{
						type:'numberbox',
						options:{
							precision:2
						}
					}},
					{title:'现场利旧',width:200,align:'center',colspan:2},
					{title:'施工队',width:200,align:'center',colspan:2},
					{title:'订单工余料',field:'order_left',width:150,align:'center',rowspan:2,formatter:function(value,rowData,index){
							rowData.order_left = parseFloat(rowData.purchase_num)-parseFloat(rowData.purchase_use_num);
							if(isNaN(rowData.order_left)){
								return "";
							}
							return rowData.order_left;
					}},
					{title:'拆旧回收',width:200,align:'center',colspan:2},
					{title:'订单领用总额',field:'purchase_use_cost',hidden:true},
					{title:'材料经济分析',width:600,align:'center',colspan:4}
				],[
					{title:'采购数',field:'purchase_num',width:100,align:'center'},
					{title:'领用数',field:'purchase_use_num',width:100,align:'center'},
					{title:'领用数',field:'my_use',width:100,align:'center'},
					{title:'总价',field:'my_cost',width:80,align:'center'},
					{title:'利旧数',field:'use_old_num',width:100,align:'center'},
					{title:'总价',field:'use_old_cost',width:80,align:'center'},
					{title:'工余料',field:'worker_left_num',width:100,align:'center',formatter:function(value,rowData,index){
							rowData.worker_left_num = parseFloat(rowData.xc_use)-parseFloat(rowData.my_use)-parseFloat(rowData.purchase_use_num);
							if(isNaN(rowData.worker_left_num)){
								return "";
							}
							return rowData.worker_left_num;
					}},
					{title:'单价',field:'worker_left_cost',width:80,align:'center',editor:{
						type:'numberbox',
						options:{
							precision:2
						}
					}},
					{title:'回收数',field:'recovery_num',width:100,align:'center'},
					{title:'总价',field:'recovery_cost',width:80,align:'center'},
					{title:'材料采购费',field:'purchase_cost',width:150,align:'center'},
					{title:'施工队材料款',field:'worker_money',width:200,align:'center',formatter:function(value,rowData,index){
						if(rowData.name == '合计:'){
							return value;
						}
						rowData.worker_money = Math.round(parseFloat(rowData.worker_left_num)*parseFloat(rowData.worker_left_cost)*Math.pow(10,2))/Math.pow(10,2);
						return rowData.worker_money;
					}},
					{title:'实际材料成本',field:'actual_money',width:200,align:'center',formatter:function(value,rowData,index){
							if(rowData.name == '合计:'){
								return value;
							}
							rowData.actual_money = parseFloat(rowData.my_cost)+parseFloat(rowData.purchase_use_cost);
							return rowData.actual_money;
					}},
					{title:'材料管控指标',field:'target_money',width:200,align:'center',formatter:function(value,rowData,index){
							if(rowData.name == '合计:'){
								return value;
							}
							rowData.target_money = parseFloat(rowData.actual_money)-parseFloat(rowData.purchase_cost)+parseFloat(rowData.recovery_cost);
							return rowData.target_money;
					}}
				   ]],
				   rowStyler : function(index,row){
					    var assume_num = parseFloat(row.assume_num);
					    if(assume_num == 0){
					    	return 'background-color:#fff;color:black;';
					    }
			    	    var control = parseFloat(row.control)/100;
			    	    var min_num = Math.round(assume_num*(1-control)*Math.pow(10,2))/Math.pow(10,2);
			    	    var max_num = Math.round(assume_num*(1+control)*Math.pow(10,2))/Math.pow(10,2);
			    	    
						if(parseFloat(row.already_use) < parseFloat(min_num))
						{	
							return 'background-color:#0aa344;color:black;';
						}
						if(parseFloat(max_num) < parseFloat(row.already_use))
						{	
							return 'background-color:#ffee00;color:red;';
						}
						return 'background-color:#fff;color:black;';
					},
				onBeforeLoad:function(){
					$(this).datagrid('rejectChanges');
				},
				onLoadSuccess:function(data){
					showFooter();
				},
				onAfterEdit:function(){
					showFooter();
				},
				onClickCell:function(rowIndex,field,value){
					if (lastIndex != rowIndex){
						$('#table').datagrid('endEdit', lastIndex);
						$('#table').datagrid('beginEdit', rowIndex);
						var ed = $('#table').datagrid('getEditor', {index:rowIndex,field:field});
						if(ed == null){
							ed = $('#table').datagrid('getEditor', {index:rowIndex,field:'xc_use'});
						}
						$(ed.target).select();
					}
					lastIndex = rowIndex;
				},
				toolbar:[{
					id:'save',
					text:'保存修改',
					disabled:false,
					iconCls:'icon-save',
					handler:function(){
							$('#table').datagrid('acceptChanges');
							lastIndex =-1;
						}
					}]
			});
		});
		function showFooter(){
			var jsondata=$("#table").datagrid("getData");
			var purchase_cost_total = 0;
			var worker_money_total = 0;
			var actual_money_total = 0;
			var target_money_total = 0;
			for(var i=0;i<jsondata.total;i++){
				purchase_cost_total += jsondata.rows[i].purchase_cost;
				worker_money_total += jsondata.rows[i].worker_money;
				actual_money_total += jsondata.rows[i].actual_money;
				target_money_total += jsondata.rows[i].target_money;
			}
			$('#table').datagrid('reloadFooter',[
			                              	{name:'合计:',
			                              	 purchase_cost: purchase_cost_total, 
			                              	 worker_money: worker_money_total,
			                              	 actual_money: actual_money_total,
			                              	 target_money: target_money_total
			                              	 }
			                              ]);
		}
		function addGDMaterialRequest(obj){
			//自动提交数据
			$('#table').datagrid('acceptChanges');
			lastIndex =-1;
			//自动提交数据
			if($('#ff').form('validate')){
				var products="";
				var jsondata=$("#table").datagrid("getData");
				for(var i=0;i<jsondata.total;i++){
					if(products!=""){
						products+=";";
					}
					products+=jsonToStr(jsondata.rows[i]);
					var index = $("#table").datagrid("getRowIndex",jsondata.rows[i])+1;
					var assume_num = parseFloat(jsondata.rows[i].assume_num);
				    if(assume_num != 0){
			    	    var control = parseFloat(jsondata.rows[i].control)/100;
			    	    var min_num = Math.round(assume_num*(1-control)*Math.pow(10,2))/Math.pow(10,2);
			    	    
						if(parseFloat(jsondata.rows[i].already_use) < parseFloat(min_num))
						{	
							$.messager.alert('提示','第'+index+'行低于管控最小值,请调整预算！');
							return ;
						}
				    }
					if(jsondata.rows[i].xc_use == null || jsondata.rows[i].xc_use < 0){
						$.messager.alert('提示','第'+index+'行现场新用数填写不正确！');
						return ;
					}
					if(jsondata.rows[i].worker_left_num != '0'){
						if(jsondata.rows[i].worker_left_cost == null || jsondata.rows[i].worker_left_cost == "" || jsondata.rows[i].worker_left_cost < 0){
							$.messager.alert('提示','第'+index+'行施工队工余料单价填写不正确！');
							return ;
						}
					}
				}
				
				var param = "gd_id=<%=gd_id%>&products="+products;
				$(obj).hide();
				$.ajax({
					type:'POST',
					url:submitUrl,
					data:param,
					dataType:'json',
					success:function(rtJson){
						$.messager.alert("提示",rtJson.rtMsrg);
					}
				});
			}
		}
		function gdPrint(){
			openDialogResize("<%=contextPath%>/springViews/print/startBalance1Print.jsp?isSP=<%=isSP%>&status=<%=status%>&gd_id=<%=gd_id%>&gd_content_id=<%=gd_content_id%>&gd_code=<%=gd_code%>&gd_name=<%=gd_name%>&xm_code=<%=xm_code%>&xm_name=<%=xm_name%>",2000, 800);
		}
	</script>
	</head>
	<body>
		<div >
		<form id="ff">
			<div style="text-align: center;">
					<p style="font-weight: bolder;">乙供材料平衡</p>
			</div>
			<table  style="border-collapse: collapse;" width="100%" border="1px" bordercolor="#0eb83a">
				<tr style="display: none;">
					<td align="right" class="td1" nowrap>项目编号：</td>
					<td class="td2" nowrap>
						<input  id="xm_code" name="xm_code" readOnly value="<%=xm_code%>" size=20>
					</td>
					<td align="right" class="td1" nowrap>项目名称：</td>
					<td class="td2" nowrap>
						<input  id="xm_name" name="xm_name" readOnly value="<%=xm_name%>" size=40>
					</td>
				</tr>
				<tr>
					<td align="right" class="td1" nowrap>工单编号：</td>
					<td class="td2" nowrap>
						<input  id="gd_code" name="gd_code" readOnly value="<%=gd_code%>" size=20>
					</td>
					<td align="right" class="td1" nowrap>工单名称：</td>
					<td class="td2" nowrap>
						<input  id="gd_name" name="gd_name" readOnly value="<%=gd_name%>" size=40>
					</td>
				</tr>
				<tr>
					<td align="right" class="td1" nowrap>
					</td>
					<td class="td2" nowrap colspan='3'>
						<div align="left">
						<% if(com.beaut.project.util.StaticData.CLOSED != Integer.parseInt(status)) {%>
							<%if(!"1".equals(isSP)){ %>
							<a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-save',plain:false" onclick="addGDMaterialRequest(this);"><span id="submitValue">提交保存</span></a>
							<%} %>
						<%} %>
						<a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-print',plain:false" onclick="gdPrint();">打印预览</a>
						
						</div>
					</td>
				</tr>
			</table>
			</form>
			<div id="table" ></div>
		</div>
	</body>
</html>